﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
using UnityEngine.EventSystems;
using Photon;

public class PlayerController : MotionController
 {
	[HideInInspector]public Inventory inventory;
	[HideInInspector] public GameObject targetItem;
	public bool pickingUp;

	
	public override void Update() 
	{	
		CarryOutState();
		if (pickingUp)
		{
			 if(targetItem != null && agent.remainingDistance < minDistance && !agent.pathPending)
			{		
					targetItem.GetComponent<NetworkPickup> ().PickUp (inventory);//calls an rpc via server. Designed so only one person can pick up the item
					GoTo (transform.position);
			}
		}
	}

	public override void GoTo(Vector3 position)
	{	
		following = false;
		pickingUp = false;
		destination = position;
		followTarget = null;
		minDistance = defaultMinDist;
		attacker.Relax();
	}

	public override void Follow(Collider target)
	{
		following = true;
		pickingUp = false;
		minDistance = defaultMinDist;
		followTarget = target;
		followTargetID = PhotonView.Get(target.transform.root).viewID;
		attacker.Relax();
	}

	public override void Attack(LifeController lc)
	{
		if (lifeController.isDead || lc.isDead)
		{
			
		}else
		{
			following = true;
			pickingUp = false;
			followTarget = lc.col;
			followTargetID = PhotonView.Get(lc.transform.root).viewID;
			minDistance = attacker.gear.weapon.attackRange - attackDistanceBuffer;
			attacker.Target (lc);
		}
	}

	public override void PickUp(GameObject obj)
	{	
		targetItem = obj.transform.root.gameObject;
		GoTo (obj.transform.position);
		pickingUp = true;
	}

	public override void LookAt(Vector3 targetPosition)
	{	
		Vector3 pos2d = new Vector3(transform.position.x,1,transform.position.z);
		Vector3 target2d = new Vector3(targetPosition.x,1,targetPosition.z);
		transform.rotation = Quaternion.LookRotation(target2d-pos2d,new Vector3(0,1,0));
		if (lrps != null)
		{
			lrps.SendPosition();
		}
	}


}